using MediatR;
using B.S.BasicData.Infrature;
using B.S.BasicData.Domain;
using B.S.BasicData.EroorCode;

namespace B.S.BasicData.Api
{
    public class SlaughterEnterpriseInfoGetByIdHandler : IRequestHandler<SlaughterEnterpriseInfoGetByIdRequest, ApiResult<SlaughterEnterpriseInfo>>
    {
        private readonly IBaseRepository<SlaughterEnterpriseInfo> _repository;

        public SlaughterEnterpriseInfoGetByIdHandler(IBaseRepository<SlaughterEnterpriseInfo> repository)
        {
            _repository = repository;
        }

        public async Task<ApiResult<SlaughterEnterpriseInfo>> Handle(SlaughterEnterpriseInfoGetByIdRequest request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.Id <= 0)
                {
                    return ApiResult<SlaughterEnterpriseInfo>.Fail("无效的编号", ApiResultCode.Error);
                }

                var result = await _repository.GetByIdAsync(request.Id);
                
                if (result == null)
                {
                    return ApiResult<SlaughterEnterpriseInfo>.Fail("未找到对应的屠宰企业信息", ApiResultCode.NotFound);
                }

                return ApiResult<SlaughterEnterpriseInfo>.Success(result, "查询成功");
            }
            catch (Exception ex)
            {
                return ApiResult<SlaughterEnterpriseInfo>.Fail($"查询失败: {ex.Message}", ApiResultCode.Error);
            }
        }
    }
} 